package br.ita.doacoes.view.controledoacoes;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.swingBean.actions.ApplicationAction;
import org.swingBean.descriptor.BeanTableModel;
import org.swingBean.descriptor.GenericFieldDescriptor;
import org.swingBean.descriptor.TableFieldDescriptor;
import org.swingBean.descriptor.XMLDescriptorFactory;
import org.swingBean.gui.JActButton;
import org.swingBean.gui.JBeanPanel;
import org.swingBean.gui.JBeanTable;

import br.ita.doacoes.core.cadastrofamilia.dao.DAO;
import br.ita.doacoes.domain.cadastrofamilia.FamiliaNecessidades;
import br.ita.doacoes.domain.controledoacoes.DestinoItem;

/**
 * @author Lucas Amaro
 * @since 04/12/2007
 * 
 * Classe que gera o painel que permite a procura por item a partir de seu nome nas necessidades das familias
 * 
 **/

public class ConsultaItemNecessidadeTemplatePanel extends JPanel {
			
	protected JBeanPanel panelFormulario;
	protected JPanel panelBotao;	
	protected JActButton botaoFiltrar;
	protected JActButton botaoListarTodos;
	protected JActButton botaoLimpar;	
	protected JLabel labelItem;
	protected JComboBox comboTipoItem;
	protected Object currentOnForm;	
	protected BeanTableModel<FamiliaNecessidades> tableModel;
	protected JBeanTable table;
	
	private String xmlForm;
	private String xmlTable;
	private String formName;
	private String tableName;
	private Class classe;
	private DAO dao;
						
	public void init() {

		TableFieldDescriptor tableDescriptor = XMLDescriptorFactory.getTableFieldDescriptor(classe, xmlTable, tableName);
		tableModel = new BeanTableModel<FamiliaNecessidades>(tableDescriptor);
		table = new JBeanTable(tableModel);
		
		tableModel.orderByProperty("nome");
		tableModel.setBeanList(dao.getList());
		table.enableQuickEditing();

		botaoFiltrar = new JActButton("Filtrar",
				new ApplicationAction() {
					public void execute() {
						tableModel.filterContains("nome",comboTipoItem.getSelectedItem().toString());
						}
				});

		botaoListarTodos = new JActButton("Listar Todos",
				new ApplicationAction() {
					public void execute() {
						tableModel.setBeanList(dao.getList());
					}
				});
		
		botaoLimpar = new JActButton("Limpar Formulario",
				new ApplicationAction() {
					public void execute() {
						panelFormulario.cleanForm();
					}
				});
		
		
		panelBotao = new JPanel();
		panelBotao.setLayout(new GridLayout());		
		panelBotao.add(botaoFiltrar);
		panelBotao.add(botaoListarTodos);
		panelBotao.add(botaoLimpar);		
		
		GenericFieldDescriptor formdescriptor = XMLDescriptorFactory.getFieldDescriptor(classe, xmlForm, formName);
		panelFormulario = new JBeanPanel<FamiliaNecessidades>(classe, formdescriptor);
				
		
		setLayout(new BorderLayout());
		add(panelFormulario,BorderLayout.NORTH);
		add(new JScrollPane(table), BorderLayout.CENTER);
		add(panelBotao,BorderLayout.SOUTH);
		
	}
		
	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}

	public Class getClasse() {
		return classe;
	}

	public void setClasse(Class classe) {
		this.classe = classe;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getXmlForm() {
		return xmlForm;
	}

	public void setXmlForm(String xmlForm) {
		this.xmlForm = xmlForm;
	}

	public String getXmlTable() {
		return xmlTable;
	}

	public void setXmlTable(String xmlTable) {
		this.xmlTable = xmlTable;
	}	

}

